package com.by.basicweb.jsonserializer;

import cn.hutool.core.util.NumberUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * 序列化BigDecimal
 *
 * @author YanBingHao
 * @since 2024/3/21
 */
public class BigDecimalJsonSerializable extends JsonSerializer<BigDecimal> {

    @Override
    public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (Objects.isNull(value)) {
            return;
        }
        gen.writeString(NumberUtil.decimalFormat("#.00", value));
    }

}
